package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.AlarmModel;
import liuliu.throughwaring.model.PersonModel;

/**
 * 报警列表页面
 * 作者：柳伟杰 on 2016/3/28 16:43
 * 邮箱：1031066280@qq.com
 */
public class WaringActivity extends BaseActivity {
    public static WaringActivity mIntails;
    @CodeNote(id = R.id.waring_toolbar)
    TToolbar waring_toolbar;
    @CodeNote(id = R.id.no_data_ll_waring, click = "onClick")
    LinearLayout no_data_ll_waring;
    @CodeNote(id = R.id.waring_lv)
    ListView waring_lv;
    @CodeNote(id = R.id.no_data_tv_waring)
    TextView no_data_tv_waring;
    @CodeNote(id = R.id.total_num_tv)
    TextView total_num_tv;
    CommonAdapter<AlarmModel> mAdapter;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_waring);
        mIntails = this;
        waring_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mIntails.finish();
            }
        });
        init();
    }

    @Override
    public void initEvents() {
        initData();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.no_data_ll_waring://重新加载数据
                initData();
                break;
        }
    }

    /**
     * 加载模拟数据
     */
    private void init() {
        List<AlarmModel> alarms = finalDb.findAll(AlarmModel.class);
        if (alarms.size() < 1) {
            //finalDb.save(new AlarmModel("柳伟杰", "从业人员", "涉恐、布控、在逃", "131182199302281014", Utils.ReadString("PoliceId"), Utils.getRealTime()));
            finalDb.save(new AlarmModel("王小艺", "盘查", "布控、在逃", "131182198202281487", Utils.ReadString("PoliceId"), Utils.getRealTime()));
            //finalDb.save(new AlarmModel("黄老二", "出租屋入住", "涉恐", "131174199302281412", Utils.ReadString("PoliceId"), Utils.getRealTime()));
       }
    }

    /**
     * 加载预警列表数据
     */
    private void initData() {
        final List<AlarmModel> list = finalDb.findAllByWhere(AlarmModel.class, "1=1 order by createTime desc");
        total_num_tv.setText("报警总数：" + list.size() + "个");
        if (list.size() > 0) {
            no_data_ll_waring.setVisibility(View.GONE);
            waring_lv.setVisibility(View.VISIBLE);
            mAdapter = new CommonAdapter<AlarmModel>(this, list, R.layout.item_waring) {
                @Override
                public void convert(ViewHolder holder, final AlarmModel alarmModel, int position) {
                    holder.setText(R.id.xuhao_tv, (position + 1) + "");
                    holder.setText(R.id.user_name_tv, alarmModel.getPersonName());
                    holder.setText(R.id.person_type_tv, alarmModel.getPersonType());
                    holder.setText(R.id.waring_type_tv, alarmModel.getWaringType());
                    holder.setText(R.id.idcard_tv, alarmModel.getIdCard());
                    holder.setText(R.id.create_time_tv, Utils.loadTime(alarmModel.getCreateTime()));
                    if (alarmModel.getHandleTime() != null) {
                        holder.setText(R.id.detail_tv, "已处理");
                        holder.setText(R.id.detail_btn, "详情");
                    } else {
                        holder.setText(R.id.detail_tv, "待处理");
                        holder.setText(R.id.detail_btn, "处理");
                    }
                    holder.setOnClickListener(R.id.detail_btn, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra("waring_id", alarmModel.getId() + "");
                            intent.setClass(WaringActivity.this, WaringDetailActivity.class);
                            startActivityForResult(intent, 0);
                        }
                    });
                }
            };

            waring_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                    Intent intent = new Intent();
                    intent.putExtra("waring_id", list.get(position).getId() + "");
                    intent.setClass(WaringActivity.this, WaringDetailActivity.class);
                    startActivityForResult(intent, 0);
                }
            });
            waring_lv.setAdapter(mAdapter);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}
